<?php
/*
[articles_id] => 
[articles_crt_date] => 
[articles_chg_date] => 
[articles_author_id] => 
[articles_editor_id] => последний редактор
[articles_category] => Категория
[articles_title] => Заголовок
[articles_text] => Текст, HTML
*/
$table = DB::$tables['articles'];

if(isset($_REQUEST['delete_articles'], $_REQUEST['deleted_articles_id'] ) && count( $_REQUEST['deleted_articles_id'])>0){
	$sql = 'DELETE FROM '.$table.' WHERE `articles_id` IN '.DB::arrToSet( $_REQUEST['deleted_articles_id'] );
	exec_sql($sql);
	Page::message( 'success', null, Lang::get('OPERATION_IS_EXECUTED').': '.'Удалено записей: '. count( $_REQUEST['deleted_articles_id']) );
}

/**/$whereStr = '';
	if(isset($_REQUEST['articles_title']) && strlen($_REQUEST['articles_title'])>0)  $whereStr.= ' AND a.articles_title LIKE "%'.db_escape( $_REQUEST['articles_title']).'%"';
	if(!empty($_REQUEST['articles_category']))  $whereStr.= ' AND a.articles_category="'.db_escape( $_REQUEST['articles_category']).'"';
	if(isset($_REQUEST['articles_publicated']) && strlen($_REQUEST['articles_publicated'])>0)  
		$whereStr.= ' AND a.articles_publicated="'.db_escape( $_REQUEST['articles_publicated']).'"';

	if(!empty($_REQUEST['articles_crt_date_from']))	$whereStr.= ' AND TO_DAYS(a.articles_crt_date)>=TO_DAYS("'.db_escape( $_REQUEST['articles_crt_date_from'] ).'")';
	if(!empty($_REQUEST['articles_crt_date_to']))	$whereStr.= ' AND TO_DAYS(a.articles_crt_date)<=TO_DAYS("'.db_escape( $_REQUEST['articles_crt_date_to'] ).'")';
	if(!empty($_REQUEST['articles_chg_date_from']))	$whereStr.= ' AND TO_DAYS(a.articles_chg_date)>=TO_DAYS("'.db_escape( $_REQUEST['articles_chg_date_from'] ).'")';
	if(!empty($_REQUEST['articles_chg_date_to']))	$whereStr.= ' AND TO_DAYS(a.articles_chg_date)<=TO_DAYS("'.db_escape( $_REQUEST['articles_chg_date_to'] ).'")';
	
	$alltable = exec_sql2array( 'SELECT '.
		'a.articles_id, '.
		'a.articles_crt_date, '.
		'a.articles_chg_date, '.
		'a.articles_author_id, '.
		'a.articles_editor_id, '.
		'a.articles_category, '.
		'a.articles_publicated, '.		
		'a.articles_title, '.
		'a.articles_text '.
		//', uda.ud_name AS author_name, '.
		//'uda.ud_surname AS author_surname, '.
		//'ud.ud_name AS editor_name, '.
		//'ud.ud_surname AS editor_surname '.
	' FROM '.$table.' AS a WHERE 1 '.$whereStr);
	//.', '.DB::$tables['user_data'].' AS uda'.', '.DB::$tables['user_data'].' AS ud'.
	//' WHERE ud.ud_user_id=a.articles_editor_id AND uda.ud_user_id=a.articles_author_id'  

	$cols=exec_sql2array( 'SELECT COLUMN_NAME, COLUMN_COMMENT '.
		'FROM information_schema.columns '.
		'WHERE table_name = \''.$table.'\' AND table_schema = \''.DB_NAME.'\'');
		
	foreach($cols as $colArr) $colTitle[$colArr['COLUMN_NAME']] = $colArr['COLUMN_COMMENT']; 
	//myPrint($alltable,'$alltable'); myPrint($colTitle,'$colTitle'); 
	
	if(count($alltable)>0){
		/*
		foreach( $alltable[0] as $name=>$value) {
			$title = !empty($colTitle[$name]) ? $colTitle[$name] : $name;
			switch($name){
				case 'page_load_from_db':
					$title='<span title="'.$title .'">'.'From DB'.'</span>';
				break;			
			}
			$th.='<th>'.$title.'</th>'."\n";
		}*/
		
		$th=''; $ths='';$trs=''; $num=0;
		foreach($alltable as $pageArr){
			 $tr=''; $num++;
			foreach($pageArr as $name=>$value) {
				//if( $name=='articles_author_id' || $name=='articles_editor_id') continue;
				$aId = $pageArr['articles_id'];
				
				switch($name){
					case 'articles_id':
						$value.= '<input type="checkbox" name="deleted_articles_id[]" value="'.$aId.'">';
					break;
					/*case 'articles_crt_date':
						$value.= '<br/>creator ID: '.$pageArr['articles_author_id'];
					break;
					case 'articles_chg_date':
						$value.= '<br/>editor ID: '.$pageArr['articles_editor_id'];
					break;*/
					case 'articles_publicated':
						$value= $pageArr['articles_publicated'] ? '<b class="success">да</b>': '<b class="warning">нет</b>';
						$onclickWO = 'onclick="window.open( this.href); return false;"';//, \'admin_popur_edit\', \'width=600,height=800\'
						$value= '<a '.$onclickWO .' title="'.Lang::get('TO_ADJUST').'..." class="setting_but_" href="/admin/modules/content/ArticleProperty.php?articles_id='.$aId.'">'.$value.'</a>';
					break;
					case 'articles_title':
						$value = '<a title="'.Lang::get('TO_SHOW').'..." href="/news/'.$aId.'">'.$value.'</a>';
					break;
					case 'articles_text': 
						//$value = highlightHtml( $value, 1 );
						$value = strip_tags( $value ); 
						$preShow = strpos($value,' ', 50); $preShow = $preShow?$preShow:50;
						$value = '<div onclick="var el=document.getElementById(\'atext'.$aId.'\'),sd=el.style.display; el.style.display=sd==\'\'?\'none\':\'\';" style="cursor:pointer" title="развернуть/свернуть">'.
							nl2br( substr($value, 0, $preShow )).
							'<span id="atext'.$aId.'" style="display:none;">'.nl2br( substr($value, $preShow )).'</span> <b>...</b>'.//
						'</div>'; 
					break;
				}
				
				$tr.='<td>'.$value.'</td>'."\n";
				
				if($ths==''){
					$title = !empty($colTitle[$name]) ? $colTitle[$name] : $name;
					switch($name){
						case 'page_load_from_db':
							$title='<span title="'.$title .'">'.'From DB'.'</span>';
						break;			
					}
					$th.='<th>'.$title.'</th>'."\n";				
				}
			}
			$trs.='<tr class="'.( $num%2?'':'odd' ).'" >'.$tr.'</tr>'."\n";
			$ths = $th;
		}
		
		$outTable = '<form method="post" name="articles_edit_form" action="">'.
			'<table class="admin_table"><thead>'.$ths.'</thead><tbody>'.$trs.'</tbody></table>'.
			'<input type="checkbox" name="check_all" value="deleted_articles_id[]">'.
			'Отмеченные: '.
			'<input type="submit" class="confirm_submit" name="delete_articles" value="'.Lang::get('TO_DELETE').'">'.
		'</form>';		
	}	
?>
<?php echo Page::outMessages(); //echo Form::outErrors( articles_form );?>

<button onclick=";var el=document.getElementById('articles_select_panel'), sd=el.style.display; el.style.display=sd==''?'none':'';" title="развернуть/свернуть">Поиск...</button><br>
<div class="panel inline-block" id="articles_select_panel" style="display: none">
	<form method="post" name="articles_select_form" action="">
	<p>
		Заголовок <input type="text" name="articles_title" id="articles_title" value="<?echo Form::requested('articles_title');?>" title="Заголовок" >
	</p><p>	
		Категория <select name="articles_category" id="articles_category" title="Категория">
			<?php
			echo Form::createSelectOptions( 
					array(''=>'', 'news'=>'news', 'article'=>'article'), 
					( isset( $_REQUEST['articles_category'] ) ? $_REQUEST['articles_category'] : '')
				);
			?>		
		</select>
	</p><p>	
		Публикация <select name="articles_publicated" id="articles_publicated" value="" title="Публикация">
			<?php
			echo Form::createSelectOptions( 
					array(''=>'', '1'=>'Публикуется', '0'=>'Не публикуется'), 
					( isset( $_REQUEST['articles_publicated'] ) ? $_REQUEST['articles_publicated'] : '')
				);
			?>
			</select>
	</p><p>
		Создано 
		от <input type="text" name="articles_crt_date_from" id="articles_crt_date_from" value="<?echo Form::requested('articles_crt_date_from');?>" title="формат: гггг-мм-дд">
		до <input type="text" name="articles_crt_date_to" id="articles_crt_date_to" value="<?echo Form::requested('articles_crt_date_to');?>" title="формат:гггг-мм-дд">
	</p><p>
		Изменено  
		от <input type="text" name="articles_chg_date_from" id="articles_chg_date_from" value="<?echo Form::requested('articles_chg_date_from');?>" title="формат:гггг-мм-дд">
		до <input type="text" name="articles_chg_date_to" id="articles_chg_date_to" value="<?echo Form::requested('articles_chg_date_to');?>" title="формат:гггг-мм-дд">
	</p><p>	
		<input type="submit" name="submit" value="<?php echo Lang::get('TO_FIND');?>">
		<input type="button"  name="reset" value="<?php echo Lang::get('TO_RESET');?>" onclick="formLib.clearValues( this.form, 0, ['submit','reset'] )">
	</p>
	</form>
	<script type="text/javascript">
	var formRules = {};
	formRules['articles_crt_date_from']=formRules['articles_crt_date_to']=formRules['articles_chg_date_from']=formRules['articles_chg_date_to'] = { 
		label:'', 
		required: 0, 
		regexp: /^\d{4}-\d{2}-\d{2}$/,
		e_message: 'формат гггг-мм-дд',
		filter:{
			regexp: /^\d{0,4}-?\d{0,2}-?\d{0,2}$/,
			mask: '####-##-##'
		}
	};
	formLib.activateOnBlurElems( document.forms['articles_select_form'], formRules );
	formLib.activateFilters( document.forms['articles_select_form'], formRules, function( form, inpName, insSym, isError){
		var messStr = isError? 'символ "'+insSym+'" не подходит': '' ;
		formLib.outMess( form, inpName, messStr, formLib.errorClass );
	});
	</script>
</div>
<hr>
<?php if(isset($outTable)) echo $outTable;?>